package action;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

import common.Pager;
import dao.TeacherDAO;
import model.Teacher;

public class TeacherAction extends ActionSupport {
	
	TeacherDAO dao=new TeacherDAO();
	private Teacher teacher;
	private int page;
	
	public Teacher getTeacher() {
		return teacher;
	}

	public void setTeacher(Teacher teacher) {
		this.teacher = teacher;
	}
	
	public int getPage() {
		return page;
	}

	public void setPage(int page) {
		this.page = page;
	}

	public String main() {	
		HttpServletRequest req=ServletActionContext.getRequest();
		//保存、显示教室的所有信息
		if(page==0) {
			page=1;
		}
		
		Pager pager=new Pager();
		dao.initPage(pager);//拿到了最新的总页数
		//|如果当前页大于总页数，则等于总页数
		if(page>pager.getPageCount()) {	//防删除最后一条数据，传过来的page是已不存在的
			page=pager.getPageCount();
		}
		if(ServletActionContext.getContext().get("ADD")!=null) {//添加用户，使跳转在最新的一页
			page=pager.getPageCount();
		}
		pager.setPageNo(page);//归类
		StringBuilder str=new StringBuilder();//变量String,可先保存，后一次性打印
		List<Teacher> list=dao.getTeachers(pager);
				
		if(list.size()>0) {
			ActionContext tex=ServletActionContext.getContext();
			tex.put("TEACHERS", list);
			
			//构建、显示导航按钮
			int btns=pager.getBtnCount();//得到当前页的所有页数按钮
			btns=btns/2;//把当前页的所有按钮平均分
				//|page是用户选择的按钮，start和end加起来等于当页的所有按钮
			int start=page-btns;//在当前按钮的找按钮的到开始点
			int end=page+btns;//..找到..终点
						
			if(start<1) {
				start=1;
				end=start+2*btns;//start<1 使后面得到扩展
			}
			if(end>pager.getPageCount()) {
				end=pager.getPageCount();
				start=end-2*btns;//end>pager.getPageCount() 后面无法继续加，使前面得到增加
			}
			if(start<1) {
				start=1;
			}
			for(int i=start;i<=end;i++) {
					//|当用户点击某个按钮时，把点击的参数page传过去
				str.append("<a href='"+req.getContextPath()+"/teacher/main?page="+i+"'>");
					//|区别当前点击的按钮和其他按钮
				if(i==page) {
					str.append("<font color='red'><b>");
					str.append(i);
					str.append("</b></font>");
				}
				else {
					str.append(i);
				}
				str.append("</a>");
				str.append("&nbsp;&nbsp;");
			}
			str.append("<br/>");
			tex.put("NAVBAR", str.toString());
		}
		return "teachermain";
	}
	
	private String updateit() {
		if(teacher.getId()==0) {//判断是修改还是添加
			ServletActionContext.getContext().put("ADD", true);
		}
		dao.update(teacher);
		return main();
	}

	public String update() {//修改和添加
		return updateit();
	}

	public String modify() {//防拦截
		return updateit();		
	}
	
	//删除教师
	public String delete() {
		dao.delete(teacher);
		return null;
	}
	
	//得到id-修改教师
	public String get() {
		teacher=dao.get(teacher);
		HttpServletResponse res=ServletActionContext.getResponse();
		res.setCharacterEncoding("utf-8");//使用过滤器，注解的方式不好用，web.xml
		try {			
			PrintWriter out=res.getWriter();
			out.print(teacher.getId()+"!"+teacher.getName()+"!"+teacher.getSex()+"!"+teacher.getPhone());
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}
}
